/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef MLIR_HLO_GML_ST_TRANSFORMS_TILING_TILING_H
#define MLIR_HLO_GML_ST_TRANSFORMS_TILING_TILING_H

#include <functional>

#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/SCF/Transforms/TileUsingInterface.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Interfaces/TilingInterface.h"

namespace mlir::gml_st {

// Creates SCFTilingOptions from the list of tile sizes.
scf::SCFTilingOptions getSCFTilingOptions(ArrayRef<int64_t> tileSizes);

/// Returns `failure`, when there occurs a problem during tiling. If the tile
/// sizes are smaller then the iteration domain of the op, it will still create
/// an `scf.forall` op. This is matches the behavior of tiling to `scf.for`
/// upstream.
struct GMLSTTilingResult {
  SmallVector<Operation *> tiledOps;
  scf::ForallOp loop = nullptr;
};
FailureOr<GMLSTTilingResult> tileUsingSCFForallOp(
    PatternRewriter &rewriter, TilingInterface op,
    const scf::SCFTilingOptions &options);

/// Extracts all yielded values from scf.in_parallel terminator. It should be
/// upstreamed.
SmallVector<Value> getYieldedValues(scf::InParallelOp inParallelOp);

}  // namespace mlir::gml_st

#endif  // MLIR_HLO_GML_ST_TRANSFORMS_TILING_TILING_H
